package com.ask.service.Impl;

import com.ask.constant.StatusConstant;
import com.ask.dto.DishDTO;
import com.ask.dto.DishQueryDTO;
import com.ask.entity.Dish;
import com.ask.entity.DishFlavor;
import com.ask.exception.BaseException;
import com.ask.mapper.DishFlavorMapper;
import com.ask.mapper.DishMapper;
import com.ask.result.PageResult;
import com.ask.service.DishService;
import com.ask.vo.DishVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl  implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveDish(DishDTO dishDTO) {
        // 1. 新增菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // todo: api
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(1L);

        dishMapper.insert(dish);

        // 2. 修改dish_flavor表
        List<DishFlavor> flavorList = dishDTO.getFlavors();

        // 批量插入dishFlavor表
        if (flavorList != null && flavorList.size() > 0) {
            for (DishFlavor dishFlavor : flavorList) {
                dishFlavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insertBatch(flavorList);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        // 1.获取菜品信息 select * from dish where id = #{id}
        Dish dish = dishMapper.getById(id);
        // 2. 根据菜品id查询口味信息：select * from dish_flavor where dish_id = #{id}
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        // 3. 封装DishVo返回前端
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public PageResult pageQuery(DishQueryDTO dishQueryDTO) {
        PageHelper.startPage(dishQueryDTO.getPage(), dishQueryDTO.getPageSize());
        Page<DishVO> pageList = dishMapper.pageQuery(dishQueryDTO);
        return new PageResult(pageList.getTotal(), pageList.getResult());
    }

    @Override
    public List<Dish> getByCategory(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishMapper.list(dish);
        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        // 1. 获取所有菜品信息
        List<Dish> dishList = dishMapper.list(dish);
        List<DishVO> dishVOList = new ArrayList<>();
        // 2. 封装dishVOList
        dishList.forEach(d -> {
            // 查询菜品的flavor信息
            List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(d.getId());
            // 封装dishVO对象, 并加入dishVOList
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            dishVO.setFlavors(dishFlavors);
            dishVOList.add(dishVO);
        });
        // 3. 返回dishVOList
        return dishVOList;
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        // 跟新菜品表
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
        // todo 跟新套餐表
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        // 1. 跟新菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        // 2. 跟新口味表
        // 2.1 删除旧数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        // 2.2 插入新数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public void deleteBatch(List<Long> ids) {
        // 禁止删除正在出售的菜品
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE)
                throw new BaseException("菜品出售中，无法删除");
        });
        // todo 禁止删除套擦中的菜品
        // 跟新菜品表
        dishMapper.deleteBatch(ids);
        // 跟新口味表
        dishFlavorMapper.deleteByDishIdBatch(ids);

    }
}
